package com.dongdongshop.service.impl;


import com.dongdongshop.mapper.TbUserMapper;
import com.dongdongshop.model.TbUser;
import com.dongdongshop.service.UserService;
import com.dongdongshop.uitl.ShiroUtils;
import com.dongdongshop.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private TbUserMapper um;

    //注册

    @Override
    public void register(UserVo vo) {
        TbUser user = new TbUser();
        BeanUtils.copyProperties(vo,user);
        //生成盐
        String salt = ShiroUtils.generateSalt(10);
        //加密
        String md5 = ShiroUtils.encryptPassword("MD5", vo.getPassword(), salt, 10);
        user.setPassword(md5);
        user.setSalt(salt);
        user.setCreated(new Date());
        user.setUpdated(new Date());
        user.setStatus("Y");

        um.insertSelective(user);

    }
    
    //登录

    @Override
    public UserVo getUserByName(String username) {
        TbUser user = um.getUserByName(username);
        UserVo vo = new UserVo();
        BeanUtils.copyProperties(user,vo);
        return vo;
    }
}
